package com.example.lion.iot;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class SwitchActivity extends AppCompatActivity {

    private boolean flag = true;

    // mqtt 参数
    private String broker = "tcp://svu_android_2017.mqtt.iot.gz.baidubce.com:1883";
    private String userID = "DeviceId-20117320131";
    private String userName = "svu_android_2017/switch";
    private String password = " ";
    private int qos = 0;
    MemoryPersistence persistence = new MemoryPersistence();

    private String topic = "switch/1";


    private MqttConnectOptions mqttCon;
    private MqttClient mqttClient;
    private MqttMessage mqttmsg;


    // http://www.eclipse.org/paho/clients/java/
    private void mqttConnection() {
        mqttCon = new MqttConnectOptions();
        mqttCon.setCleanSession(true);
        mqttCon.setConnectionTimeout(30);
        mqttCon.setKeepAliveInterval(60);
        mqttCon.setUserName(userName);
        mqttCon.setPassword(password.toCharArray());

        try {
            mqttClient = new MqttClient(broker, userID, persistence);
            mqttClient.connect(mqttCon);

            Toast.makeText(SwitchActivity.this,"MQTT 已连接",Toast.LENGTH_SHORT).show();


        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }


    }

    private void sendMqttSwitchState(boolean state) {

        try {
            if(state==true){
                mqttmsg = new MqttMessage("开启".getBytes());
            }else {
                mqttmsg = new MqttMessage("关闭".getBytes());
            }

            mqttmsg.setQos(qos);

            mqttClient.publish(topic, mqttmsg);


        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            mqttClient.disconnect();
            Toast.makeText(SwitchActivity.this,"MQTT 已断开",Toast.LENGTH_SHORT).show();
        }catch (MqttException e){

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);

        final Button btnSwitchState = (Button) findViewById(R.id.btn_switch_state);
        final TextView txtSwitchState = (TextView) findViewById(R.id.txt_switch_state);

        mqttConnection();

        // http://www.eclipse.org/paho/clients/android/
        // https://github.com/eclipse/paho.mqtt.android

        btnSwitchState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (flag == true) {
                    btnSwitchState.setBackgroundResource(R.drawable.on);
                    txtSwitchState.setText("当前状态：开启");


                    sendMqttSwitchState(true);

                } else {
                    btnSwitchState.setBackgroundResource(R.drawable.off);
                    txtSwitchState.setText("当前状态：关闭");

                    sendMqttSwitchState(false);
                }

                flag = !flag;

            }
        });
    }
}
